// pages/staff_mission/index/index.js

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        map_latitude: 23.099994,
        map_longitude: 113.324520,
        map_markers: [],
        map_points: [],
        map_height: 600,
        get_mission: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        // 验证登录
        app.verifyLogin();

        // 获取当前定位
        app.execute(function() {
            let page = app.getPage();
            page.getNowLocation();
            page.getMissionList();
        });
    },

    /**
     * 生命周期函数--监听页面完成
     */
    onReady: function() {

        // 更新地图高度
        let page = this;
        wx.createSelectorQuery().select('.foot').fields({
            size: true
        }, function(response) {

            let height_window = wx.getSystemInfoSync().windowHeight;
            let height_footer = response.height;

            page.setData({
                map_height: height_window - height_footer,
            });

        }).exec();

    },

    /**
     * 自定义函数--获取当前定位
     */
    getNowLocation: function() {

        let page = app.getPage();
        wx.showLoading({
            title: '定位中...',
        });
        wx.authorize({
            scope: "scope.userLocation",
            success: function(e) {
                wx.hideLoading();
                wx.getLocation({
                    type: 'gcj02',
                    altitude: 'true',
                    success: function(e) {
                        let change = new Object();
                        change['map_latitude'] = e.latitude;
                        change['map_longitude'] = e.longitude;
                        change['get_mission'] = true;
                        change.map_points = page.data.map_points;
                        change.map_points.push({
                            latitude: e.latitude,
                            longitude: e.longitude,
                        });
                        page.setData(change);
                    },
                    fail: function() {
                        wx.showToast({
                            title: '获取位置失败',
                            icon: 'none'
                        });
                    }
                });
            },
            fail: function() {
                wx.hideLoading();
                wx.showModal({
                    title: "提示",
                    content: "由于您禁用了小程序的权限，暂时无法获取位置，请在小程序设置里重新启用",
                    success: function(e) {
                        if (true === e.confirm) {
                            wx.openSetting();
                        } else {
                            wx.showToast({
                                title: '不能获取当前位置',
                            });
                        };
                    }
                });
            }
        });

    },

    /**
     * 自定义函数--获取当前任务列表
     */
    getMissionList: function() {

        let page = app.getPage();
        if (false === this.data.get_mission) {
            setTimeout(function() {
                page.getMissionList();
            }, 500);
            return;
        };

        wx.showLoading({
            title: '获取任务...',
        });
        wx.request({
            url: app.data.service_url + 'staff_recycle_order/list_progress',
            header: {
                'Client-Token': app.data.client_token,
            },
            method: 'GET',
            data: {
                page: 0
            },
            success: function(response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (0 === response.data.errno) {
                        let change = new Object();
                        change['map_markers'] = new Array();
                        change['map_points'] = page.data.map_points;
                        for (let i = 0; i < response.data.data.order_list.length; i++) {
                            let order = response.data.data.order_list[i];
                            change['map_markers'].push({
                                id: order.id,
                                latitude: order.position_lat,
                                longitude: order.position_lng,
                                width: 20,
                                height: 20,
                                iconPath: '/images/markers.png'
                            });
                            change['map_points'].push({
                                latitude: order.position_lat,
                                longitude: order.position_lng,
                            });
                        };
                        page.setData(change);
                    } else {
                        wx.showToast({
                            title: response.data.message,
                            icon: 'none'
                        });
                    };
                } else {
                    wx.showToast({
                        title: '远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideLoading();
                wx.showToast({
                    title: '连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },

    /**
     * 自定义函数--获取地图坐标
     */
    getMapMarkers: function(e) {
        wx.navigateTo({
            url: '/pages/staff_mission/detail/index?id=' + e.markerId,
        });

    },

    /**
     * 自定义函数--扫描二维码
     */
    clickToScan: function() {
        wx.showLoading({
            title: '扫码中...',
        });
        wx.scanCode({
            onlyFromCamera: true,
            scanType: ['qrCode'],
            success: function(e) {
                wx.hideLoading();
                if ('scanCode:ok' === e.errMsg) {
                    wx.showLoading({
                        title: '准备跳转...',
                    });
                    wx.navigateTo({
                        url: '/pages/staff_mission/scanning/index?id=' + e.result,
                    });
                } else {
                    wx.showToast({
                        title: '内容不符合',
                        icon: 'none'
                    });
                };
            },
            fail: function() {
                wx.hideLoading();
            }
        });
    }

});